﻿/*
 * Login.java
 *
 * Created on __DATE__, __TIME__
 */

package scrn;


import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.imageio.ImageIO;

import core.*;


/**
 * 
 * @author __USER__
 */
public class Login extends javax.swing.JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;


/** Creates new form Login */
public Login() {

	BufferedImage img = null;
	try {

		URL u = getClass().getResource("/res/icon.png");
		img = ImageIO.read(u);

	} catch (Exception e) {
	}

	if (img != null) {
		this.setIconImage(img);
		img = null;
	}

	initComponents();

	if (Setting.login != null) {
		this.tfLogin.setText(Setting.login);
	}

	if (Setting.password != null) {
		this.tfPass.setText(Setting.password);
	}
}


//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

	tfLogin = new javax.swing.JTextField();
	tfPass = new javax.swing.JTextField();
	butOk = new javax.swing.JButton();
	jLabel1 = new javax.swing.JLabel();
	jLabel2 = new javax.swing.JLabel();
	jLabel3 = new javax.swing.JLabel();
	jLabel4 = new javax.swing.JLabel();
	labHint = new javax.swing.JLabel();
	butShowPass = new javax.swing.JToggleButton();

	setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
	setTitle("jChat.\u0412\u0445\u043e\u0434");
	setResizable(false);

	tfLogin.addKeyListener(new java.awt.event.KeyAdapter() {

		public void keyPressed(java.awt.event.KeyEvent evt) {

			tfLoginKeyPressed(evt);
		}
	});

	tfPass.addKeyListener(new java.awt.event.KeyAdapter() {

		public void keyPressed(java.awt.event.KeyEvent evt) {

			tfPassKeyPressed(evt);
		}
	});

	butOk.setText("OK");
	butOk.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
	butOk.addActionListener(new java.awt.event.ActionListener() {

		public void actionPerformed(java.awt.event.ActionEvent evt) {

			butOkActionPerformed(evt);
		}
	});

	jLabel1.setText("\u0412\u0430\u0448 \u043d\u0438\u043a (\u0438\u043c\u044f)");

	jLabel2.setText("\u0412\u0430\u0448 \u043f\u0430\u0440\u043e\u043b\u044c");

	jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
	jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	jLabel3
			.setText("\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435\u0441\u044c, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430!");

	labHint.setForeground(new java.awt.Color(204, 0, 0));
	labHint.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	labHint.setText("          ");

	butShowPass.setBorder(null);
	butShowPass.setBorderPainted(false);
	butShowPass.setContentAreaFilled(false);
	butShowPass.setMargin(new java.awt.Insets(0, 0, 0, 0));
	butShowPass.setMaximumSize(new java.awt.Dimension(24, 24));
	butShowPass.setMinimumSize(new java.awt.Dimension(24, 24));
	butShowPass.setPreferredSize(new java.awt.Dimension(24, 24));

	javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	getContentPane().setLayout(layout);
	layout.setHorizontalGroup(layout.createParallelGroup(
			javax.swing.GroupLayout.Alignment.LEADING).addGroup(
			layout.createSequentialGroup().addContainerGap().addComponent(jLabel4,
					javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE).addGap(
					237, 237, 237)).addGroup(
			layout.createSequentialGroup().addContainerGap().addComponent(labHint,
					javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
					.addContainerGap()).addGroup(
			layout.createSequentialGroup().addGap(26, 26, 26).addGroup(
					layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
							.addComponent(jLabel2).addComponent(jLabel1)).addGap(18, 18, 18)
					.addGroup(
							layout.createParallelGroup(
									javax.swing.GroupLayout.Alignment.TRAILING, false).addGroup(
									layout.createSequentialGroup().addComponent(tfPass)
											.addPreferredGap(
													javax.swing.LayoutStyle.ComponentPlacement.RELATED)
											.addComponent(butShowPass,
													javax.swing.GroupLayout.PREFERRED_SIZE, 27,
													javax.swing.GroupLayout.PREFERRED_SIZE))
									.addComponent(tfLogin,
											javax.swing.GroupLayout.Alignment.LEADING,
											javax.swing.GroupLayout.PREFERRED_SIZE, 235,
											javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(28, 28,
							28)).addGroup(
			layout.createSequentialGroup().addGap(138, 138, 138).addComponent(butOk,
					javax.swing.GroupLayout.PREFERRED_SIZE, 105,
					javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(134,
					Short.MAX_VALUE)).addGroup(
			layout.createSequentialGroup().addContainerGap().addComponent(jLabel3)
					.addContainerGap(203, Short.MAX_VALUE)));
	layout.setVerticalGroup(layout.createParallelGroup(
			javax.swing.GroupLayout.Alignment.LEADING).addGroup(
			layout.createSequentialGroup().addContainerGap().addComponent(jLabel3)
					.addGap(28, 28, 28).addGroup(
							layout.createParallelGroup(
									javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
									jLabel1).addComponent(tfLogin,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									javax.swing.GroupLayout.DEFAULT_SIZE,
									javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(18, 18, 18)
					.addGroup(
							layout.createParallelGroup(
									javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
									jLabel2).addComponent(butShowPass,
									javax.swing.GroupLayout.PREFERRED_SIZE, 23,
									javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(tfPass,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									javax.swing.GroupLayout.DEFAULT_SIZE,
									javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(31, 31, 31)
					.addComponent(jLabel4).addPreferredGap(
							javax.swing.LayoutStyle.ComponentPlacement.RELATED,
							javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(labHint).addGap(8, 8, 8).addComponent(butOk)
					.addContainerGap()));

	pack();
}// </editor-fold>
//GEN-END:initComponents


private void tfPassKeyPressed(java.awt.event.KeyEvent evt) {

	int code = evt.getKeyCode();

	if (code == KeyEvent.VK_ENTER) {
		butOkActionPerformed(null);
	} else if (code == KeyEvent.VK_DOWN) {
		butOk.requestFocusInWindow();
	}
}


private void tfLoginKeyPressed(java.awt.event.KeyEvent evt) {

	int code = evt.getKeyCode();

	if (code == KeyEvent.VK_ENTER || code == KeyEvent.VK_DOWN) {
		tfPass.requestFocusInWindow();
	}
}


private boolean testCond() {

	boolean fOk = true;

	String login = tfLogin.getText();
	String pass = tfPass.getText();

	if (login.length() <= Setting.MIN_NICK_LEN) {
		labHint.setText("Логин должен быть не короче " + Setting.MIN_NICK_LEN
				+ " символов, не считая смайлы.");
		fOk = false;
	} else if (login.length() > Setting.MAX_NICK_LEN) {
		labHint.setText("Логин должен быть не длиннее " + Setting.MAX_NICK_LEN
				+ " символов, считая смайлы.");
		fOk = false;
	} else if (pass.length() < Setting.MIN_PASS_LEN) {
		labHint.setText("Пароль должен быть не короче " + Setting.MIN_PASS_LEN
				+ " символов.");
		fOk = false;
	} else if (pass.length() > Setting.MAX_PASS_LEN) {
		labHint.setText("Пароль должен быть не длиннее " + Setting.MAX_PASS_LEN
				+ " символов.");
		fOk = false;
	} else {
		Setting.password = tfPass.getText();
		Setting.login = tfLogin.getText();
		fOk = true;
	}

	return fOk;

}


private void butOkActionPerformed(java.awt.event.ActionEvent evt) {

	if (testCond()) {
		this.dispose();
		Man.main.connect();
	}
}


/**
 * @param args
 *          the command line arguments
 */
public static void main(String args[]) {

	java.awt.EventQueue.invokeLater(new Runnable() {

		public void run() {

			new Login().setVisible(true);
		}
	});
}

//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton butOk;
private javax.swing.JToggleButton butShowPass;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel labHint;
private javax.swing.JTextField tfLogin;
private javax.swing.JTextField tfPass;
// End of variables declaration//GEN-END:variables

}